package com.zhaogang.transactionManagerDemo;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 测试以下几个的初始化顺序:
 * 属性赋值
 * 构造函数执行
 * @Autowired赋值
 * @PostConstruct注解的方法
 * afterPropertiesSet方法
 * 
 * @author: jie.deng
 * @time: 2017年7月7日 上午10:18:30
 */
@Component
public class SpringBeanLifeCycle implements InitializingBean {

	@Autowired
	private DataSource ds;
	private Test test = new Test();
	
	public SpringBeanLifeCycle() {
		super();
		System.out.println("invoke constructor()");
		System.out.println("ds == null?" + ds == null);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("invoke afterPropertiesSet()");
		System.out.println("ds == null?" + ds == null);
	}

	@PostConstruct
	public void afterConstruct() throws Exception {
		System.out.println("invoke afterConstruct()");
		System.out.println("ds == null?" + ds == null);
	}
	
	public void test() {
		System.out.println("invoke test()");
		System.out.println("ds == null?" + ds == null);
	}

	public DataSource getDs() {
		System.out.println("invoke getDs()");
		System.out.println("ds == null?" + ds == null);		
		return ds;
	}

	public void setDs(DataSource ds) {
		this.ds = ds;
	}

	class Test{
		public Test(){
			System.out.println("ds == null?" + ds == null);		
			System.out.println("invoke Test constructor()");
		}
	}
}

